package Array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-29  15:38
 * @Description: TODO
 * @Version: 1.0
 */
public class 数组中众数 {

    public int[] find(int[] nums) {
        ArrayList<Integer> list = new ArrayList<>();
        int maxcount = 0;
        // 有序
        Arrays.sort(nums);
        int count = 0;
        list.add(nums[0]);
        count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] == nums[i]) {
                count++;
                if (maxcount < count) {
                    maxcount = count;
                    list.clear();
                    list.add(nums[i]);
                } else if (maxcount == count) {
                    list.add(nums[i]);
                }
            } else {
                count = 1;
            }
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    @Test
    public void test() {
        int[] ints = find(new int[]{2, 2, 5, 6, 6, 7, 7, 7, 9, 9, 9});
        System.out.println(Arrays.toString(ints));
    }
}
